home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_src.lha / WIN / WIN_CLNT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-27  |  15.5 KB  |  470 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: win_clnt.c
  4.  
  5.     PURPOSE: sample Kerberos client for Windows
  6.  
  7.     FUNCTIONS:
  8.  
  9.     WinMain() - calls initialization function, processes message loop
  10.     InitApplication() - initializes window data and registers window
  11.     InitInstance() - saves instance handle and creates main window
  12.     MainWndProc() - processes messages
  13.     About() - processes messages for "About" dialog box
  14.  
  15.     COMMENTS:
  16.  
  17.     Windows can have several copies of your application running at the
  18.     same time.  The variable hInst keeps track of which instance this
  19.     application is so that processing will be to the correct window.
  20.  
  21. ****************************************************************************/
  22.  
  23. #include "windows.h"            /* required for all Windows applications */
  24. #include "win_clnt.h"            /* specific to this program             */
  25.  
  26. #include <mit_copy.h>
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <stdlib.h>
  30. #include <netinet/in.h>
  31. #include <netdb.h>
  32. #include <krb.h>
  33.  
  34. HANDLE hInst;                /* current instance                 */
  35. char    hostname[100];
  36. int     checksum;
  37. long FAR PASCAL MainWndProc();
  38. BOOL FAR PASCAL Dialog();
  39. void authenticate(HWND,char*,long);
  40. int far printf();
  41. static int sock=0;
  42.  
  43. int stderr=2;
  44. /****************************************************************************
  45.  
  46.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  47.  
  48.     PURPOSE: calls initialization function, processes message loop
  49.  
  50.     COMMENTS:
  51.  
  52.         Windows recognizes this function by name as the initial entry point 
  53.         for the program.  This function calls the application initialization 
  54.         routine, if no other instance of the program is running, and always 
  55.         calls the instance initialization routine.  It then executes a message 
  56.         retrieval and dispatch loop that is the top-level control structure 
  57.     for the remainder of execution.  The loop is terminated when a WM_QUIT
  58.         message is received, at which time this function exits the application 
  59.         instance by returning the value passed by PostQuitMessage(). 
  60.  
  61.         If this function must abort before entering the message loop, it 
  62.         returns the conventional value NULL.  
  63.  
  64. ****************************************************************************/
  65.  
  66. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  67. HANDLE hInstance;                 /* current instance         */
  68. HANDLE hPrevInstance;                 /* previous instance         */
  69. LPSTR lpCmdLine;                 /* command line             */
  70. int nCmdShow;                     /* show-window type (open/icon) */
  71. {
  72.     MSG msg;                     /* message                 */
  73.  
  74.     if (!hPrevInstance)             /* Other instances of app running? */
  75.     if (!InitApplication(hInstance)) /* Initialize shared things */
  76.         return (FALSE);         /* Exits if unable to initialize     */
  77.  
  78.     /* Perform initializations that apply to a specific instance */
  79.  
  80.     if (!InitInstance(hInstance, nCmdShow))
  81.     return (FALSE);
  82.  
  83.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  84.  
  85.     while (GetMessage(&msg,       /* message structure                 */
  86.         NULL,           /* handle of window receiving the message */
  87.         NULL,           /* lowest message to examine             */
  88.         NULL))           /* highest message to examine         */
  89.     {
  90.     TranslateMessage(&msg);       /* Translates virtual key codes         */
  91.     DispatchMessage(&msg);       /* Dispatches message to window         */
  92.     }
  93.     return (msg.wParam);       /* Returns the value from PostQuitMessage */
  94. }
  95.  
  96.  
  97. /****************************************************************************
  98.  
  99.     FUNCTION: InitApplication(HANDLE)
  100.  
  101.     PURPOSE: Initializes window data and registers window class
  102.  
  103.     COMMENTS:
  104.  
  105.     This function is called at initialization time only if no other
  106.     instances of the application are running.  This function performs
  107.     initialization tasks that can be done once for any number of running
  108.     instances.
  109.  
  110.         In this case, we initialize a window class by filling out a data 
  111.         structure of type WNDCLASS and calling the Windows RegisterClass() 
  112.     function.  Since all instances of this application use the same window
  113.         class, we only need to do this when the first instance is initialized.  
  114.  
  115.  
  116. ****************************************************************************/
  117.  
  118. BOOL InitApplication(hInstance)
  119. HANDLE hInstance;                   /* current instance         */
  120. {
  121.     WNDCLASS  wc;
  122.  
  123.   /* Fill in window class structure with parameters that describe the       */    /* main window.                                                           */
  124.  
  125.     wc.style = NULL;                  /* Class style(s).                    */
  126.     wc.lpfnWndProc = MainWndProc;     /* Function to retrieve messages for  */
  127.                       /* windows of this class.             */
  128.     wc.cbClsExtra = 0;                /* No per-class extra data.           */
  129.     wc.cbWndExtra = 0;                /* No per-window extra data.          */
  130.     wc.hInstance = hInstance;         /* Application that owns the class.   */
  131.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  132.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  133.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  134.     wc.lpszMenuName =  "Main_Menu"; /* Name of menu resource in .RC file. */
  135.     wc.lpszClassName = "win_clntWClass"; /* Name used in call to CreateWindow. */
  136.  
  137.     /* Register the window class and return success/failure code. */
  138.  
  139.     return (RegisterClass(&wc));
  140.  
  141. }
  142.  
  143.  
  144. /****************************************************************************
  145.  
  146.     FUNCTION:  InitInstance(HANDLE, int)
  147.  
  148.     PURPOSE:  Saves instance handle and creates main window
  149.  
  150.     COMMENTS:
  151.  
  152.         This function is called at initialization time for every instance of 
  153.         this application.  This function performs initialization tasks that 
  154.         cannot be shared by multiple instances.  
  155.  
  156.         In this case, we save the instance handle in a static variable and 
  157.         create and display the main program window.  
  158.         
  159. ****************************************************************************/
  160.  
  161. BOOL InitInstance(hInstance, nCmdShow)
  162.     HANDLE          hInstance;        /* Current instance identifier.       */
  163.     int             nCmdShow;         /* Param for first ShowWindow() call. */
  164. {
  165.     HWND            hWnd;             /* Main window handle.                */
  166.  
  167.     /* Save the instance handle in static variable, which will be used in  */
  168.     /* many subsequence calls from this application to Windows.            */
  169.  
  170.     hInst = hInstance;
  171.  
  172.  
  173.     /* Create a main window for this application instance.  */
  174.  
  175.     hWnd = CreateWindow(
  176.     "win_clntWClass",             /* See RegisterClass() call.         */
  177.     "Kerberos Sample Client",  /* Text for window title bar.       */
  178.     WS_OVERLAPPEDWINDOW,            /* Window style.                    */
  179.     CW_USEDEFAULT,                  /* Default horizontal position.     */
  180.     CW_USEDEFAULT,                  /* Default vertical position.       */
  181.     CW_USEDEFAULT,                  /* Default width.                   */
  182.     CW_USEDEFAULT,                  /* Default height.                  */
  183.     NULL,                         /* Overlapped windows have no parent. */
  184.     NULL,                         /* Use the window class menu.         */
  185.     hInstance,                    /* This instance owns this window.    */
  186.     NULL                          /* Pointer not needed.                */
  187.     );
  188.  
  189.     /* If window could not be created, return "failure" */
  190.  
  191.     if (!hWnd)
  192.     return (FALSE);
  193.  
  194.     /* Make the window visible; update its client area; and return "success" */
  195.  
  196.     ShowWindow(hWnd, nCmdShow);  /* Show the window                        */
  197.     UpdateWindow(hWnd);          /* Sends WM_PAINT message                 */
  198.     return (TRUE);               /* Returns the value from PostQuitMessage */
  199.  
  200. }
  201.  
  202. /****************************************************************************
  203.  
  204.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  205.  
  206.     PURPOSE:  Processes messages
  207.  
  208.     MESSAGES:
  209.  
  210.     WM_COMMAND    - application menu (About dialog box)
  211.     WM_DESTROY    - destroy window
  212.  
  213.     COMMENTS:
  214.  
  215.     To process the IDM_ABOUT message, call MakeProcInstance() to get the
  216.     current instance address of the About() function.  Then call Dialog
  217.     box which will create the box according to the information in your
  218.     win_clnt.rc file and turn control over to the About() function.    When
  219.     it returns, free the intance address.
  220.  
  221. ****************************************************************************/
  222.  
  223. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  224. HWND hWnd;                  /* window handle             */
  225. unsigned message;              /* type of message             */
  226. WORD wParam;                  /* additional information         */
  227. LONG lParam;                  /* additional information         */
  228. {
  229.     FARPROC lpProcDialog;          /* pointer to the "Dialog" function */
  230.  
  231.     switch (message) {
  232.     case WM_COMMAND:       /* message: command from application menu */
  233.         if (wParam == IDM_GO) {
  234.         /* set_debug_window(hWnd); */
  235.         lpProcDialog = MakeProcInstance(Dialog, hInst);
  236.  
  237.         DialogBox(hInst,         /* current instance         */
  238.             "Auth_Dialog",             /* resource to use         */
  239.             hWnd,             /* parent handle         */
  240.             lpProcDialog);         /* Dialog() instance address */
  241.  
  242.         FreeProcInstance(lpProcDialog);
  243.         authenticate(hWnd,hostname,checksum);
  244.         if (sock)
  245.             soclose(sock);
  246.         break;
  247.         }
  248.         else                /* Lets Windows process it         */
  249.         return (DefWindowProc(hWnd, message, wParam, lParam));
  250.  
  251.     case WM_DESTROY:          /* message: window being destroyed */
  252.         if (sock)
  253.             soclose(sock);
  254.         PostQuitMessage(0);
  255.         break;
  256.  
  257.     default:              /* Passes it on if unproccessed    */
  258.         return (DefWindowProc(hWnd, message, wParam, lParam));
  259.     }
  260.     return (NULL);
  261. }
  262.  
  263.  
  264. /****************************************************************************
  265.  
  266.     FUNCTION: Dialog(HWND, unsigned, WORD, LONG)
  267.  
  268.     PURPOSE:  Processes messages for "Dialog" dialog box
  269.  
  270.     MESSAGES:
  271.  
  272.     WM_INITDIALOG - initialize dialog box
  273.     WM_COMMAND    - Input received
  274.  
  275.     COMMENTS:
  276.  
  277.     No initialization is needed for this particular dialog box, but TRUE
  278.     must be returned to Windows.
  279.  
  280.     Wait for user to click on "Ok" button, then close the dialog box.
  281.  
  282. ****************************************************************************/
  283.  
  284. BOOL FAR PASCAL Dialog(hDlg, message, wParam, lParam)
  285. HWND hDlg;                                /* window handle of the dialog box */
  286. unsigned message;                         /* type of message                 */
  287. WORD wParam;                              /* message-specific information    */
  288. LONG lParam;
  289. {
  290.     switch (message) {
  291.     case WM_INITDIALOG:           /* message: initialize dialog box */
  292.         return (TRUE);
  293.  
  294.     case WM_COMMAND:              /* message: received a command */
  295.         if (wParam == IDD_OK || wParam == IDD_CANCEL) {
  296.             if(wParam == IDD_OK) {
  297.                 GetDlgItemText(hDlg,IDD_HOST,(LPSTR)hostname,99);
  298.                 checksum=(int)GetDlgItemInt(hDlg,IDD_CKSUM,NULL,TRUE);
  299.             } else 
  300.                 *hostname='\0';
  301.         EndDialog(hDlg, TRUE);          /* Exits the dialog box         */
  302.         return (TRUE);
  303.         }
  304.         break;
  305.     }
  306.     return (FALSE);                  /* Didn't process a message    */
  307. }
  308. /*
  309.  * $Source: /mit/kerberos/src/appl/sample/RCS/sample_client.c,v $
  310.  * $Author: jtkohl $
  311.  *
  312.  * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
  313.  *
  314.  * For copying and distribution information,
  315.  * please see the file <mit-copyright.h>.
  316.  *
  317.  * sample_client:
  318.  * A sample Kerberos client, which connects to a server on a remote host,
  319.  * at port "sample" (be sure to define it in /etc/services)
  320.  * and authenticates itself to the server. The server then writes back
  321.  * (in ASCII) the authenticated name.
  322.  *
  323.  * Usage:
  324.  * sample_client <hostname> <checksum>
  325.  *
  326.  * <hostname> is the name of the foreign host to contact.
  327.  *
  328.  * <checksum> is an integer checksum to be used for the call to krb_mk_req()
  329.  *    and mutual authentication
  330.  *
  331.  * If DEBUG is defined, authenticate to server "test.test".
  332.  */
  333.  
  334.  
  335. #define SAMPLE_SERVICE    "sample"
  336.  
  337. #ifdef DEBUG
  338. #define    TEST_SERVICE    "test"
  339. #endif
  340.  
  341. static void farcopy(LPBYTE src,LPBYTE dst, int length)
  342. {
  343.     int i;
  344.     
  345.     for (i=length; i; i--)
  346.         *dst++=*src++;
  347. }
  348.  
  349. void authenticate(HWND hWnd,char *hostname,long cksum)
  350. {
  351.     HANDLE hsp,hhp;
  352.     struct servent far *sp;
  353.     struct hostent far *hp;
  354.     struct sockaddr_in sin, lsin;
  355.     char *remote_host;
  356.     int status;
  357.     int namelen;
  358.     KTEXT_ST ticket;
  359.     char buf[512];
  360.     long authopts;
  361.     MSG_DAT msg_data;
  362.     CREDENTIALS cred;
  363.     Key_schedule sched;
  364.     long host_ip;
  365.     LPSTR env;
  366.  
  367.     set_krb_debug(1);
  368.     set_ap_req_debug(1); 
  369.  
  370.     /* clear out the structure first */
  371.     (void) bzero((char *)&sin, sizeof(sin));
  372.  
  373.     /* find the port number for knetd */
  374.     hsp = getservbyname(SAMPLE_SERVICE, "tcp");
  375.     if (!hsp) {
  376.     printf(
  377.         (LPSTR)"unknown service %s/tcp; check /etc/services\n",
  378.         (LPSTR)SAMPLE_SERVICE);
  379.     return;
  380.     }
  381.     sp=(struct servent far*)GlobalLock(hsp);
  382.     /* copy the port number */
  383.     sin.sin_port = sp->s_port;
  384.     GlobalUnlock(hsp);
  385.     GlobalFree(hsp);
  386.     sin.sin_family = AF_INET;
  387. #if 1
  388.     /* look up the server host */
  389.     hhp = gethostbyname(hostname);
  390.     if (!hhp) {
  391.     printf((LPSTR) "unknown host %s\n",(LPSTR)hostname);
  392.     return;
  393.     }
  394.     hp=(struct hostent far*)GlobalLock(hhp);
  395.     /* copy the hostname into non-volatile storage */
  396.     remote_host = malloc(lstrlen(((LPSTR)hp)+hp->h_name) + 1);
  397.     (void) lstrcpy((LPSTR)remote_host, ((LPSTR)hp)+hp->h_name);
  398.  
  399.     /* set up the address of the foreign socket for connect() */
  400.     sin.sin_family = hp->h_addrtype;
  401.     farcopy(((LPBYTE)hp)+hp->h_addr,
  402.          (LPBYTE)&sin.sin_addr,
  403.          hp->h_length);    
  404. #else    /* DNS version */
  405.     printf("Before rhost\n");
  406.     /* copy the hostname into non-volatile storage */
  407.     remote_host = malloc(strlen(hostname + 1);
  408.     (void) strcpy(remote_host, hostname);
  409.     host_ip=rhost(&hostname);
  410.     sin.sin_family=AF_INET;
  411.     sin.sin_addr.s_addr=host_ip;
  412. #endif
  413.     GlobalUnlock(hhp);
  414.     GlobalFree(hhp);
  415.     /* open a TCP socket */
  416.     sock = socket(PF_INET, SOCK_STREAM, 0);
  417.     if (sock < 0) {
  418.         printf((LPSTR)"socket : Error %d\n",GetErrno());
  419.     return;
  420.     }
  421.     
  422.     /* connect to the server */
  423.     if (connect(sock,(struct sockaddr*) &sin, sizeof(sin)) < 0) {
  424.         printf((LPSTR)"connect : Error %d\n", GetErrno());
  425.     return;
  426.     }
  427.  
  428.     /* find out who I am, now that we are connected and therefore bound */
  429.     namelen = sizeof(lsin);
  430.     if (getsockname(sock, (struct sockaddr *) &lsin, &namelen) < 0) {
  431.         printf((LPSTR)"getsockname : error %d\n", GetErrno());
  432.     return;
  433.     }
  434.  
  435.     /* call Kerberos library routine to obtain an authenticator,
  436.        pass it over the socket to the server, and obtain mutual
  437.        authentication. */
  438.  
  439.     authopts = KOPT_DO_MUTUAL;
  440.     status = krb_sendauth(authopts, sock, &ticket,
  441. #ifdef DEBUG
  442.               TEST_SERVICE, TEST_SERVICE,
  443. #else
  444.               SAMPLE_SERVICE, remote_host,
  445. #endif
  446.               NULL, cksum, &msg_data, &cred,
  447.               &sched[0], &lsin, &sin, "VERSION9");
  448.     if (status != KSUCCESS) {
  449.     printf((LPSTR) "%s: cannot authenticate to server: %s\n",
  450.         (LPSTR)"win_clnt", (LPSTR)get_krb_err_txt()[status]);
  451.     return;
  452.     }
  453.  
  454.     /* After we send the authenticator to the server, it will write
  455.        back the name we authenticated to. Read what it has to say. */
  456.     status = soread(sock, buf, 512);
  457.     if (status < 0) {
  458.     printf((LPSTR)"read : Error %d", GetErrno);
  459.     return;
  460.     }
  461.  
  462.     /* make sure it's null terminated before printing */
  463.     if (status < 512)
  464.     buf[status] = '\0';
  465.     MessageBox(hWnd,(LPSTR)buf,(LPSTR)"Server Reply",
  466.                 MB_OK|MB_ICONINFORMATION);
  467.         
  468. }
  469.  
  470.